<?php

namespace app\admin\controller;


use app\admin\model\PurchaseSub;
use cmf\controller\AdminBaseController;
use think\facade\Db;


class PurchasesubController extends AdminBaseController
{
                /**
              *列表
              * @adminMenu(
              *     'name'   => '采购合同子项',
              *     'parent' => 'admin/Purchase/index',
              *     'display'=> true,
              *     'hasView'=> false,
              *     'order'  => 10000,
              *     'icon'   => '',
              *     'remark' => '',
              *     'param'  => ''
              * )
            */
    public function index(){
        //搜索部分
        $purchase_id = input('get.id/d');

        $lists = PurchaseSub::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
        $lists = $lists->where('purchase_id', $purchase_id);
        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);

        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(),'domain'=>request()->domain().'/upload/']);
    }

    public function add(){
        
        return $this->fetch('',['domain'=>request()->domain().'/upload/']);
    }

    public function addPost(){
        $params = array_map(function($v){
            return is_array($v)?implode(',',$v):$v;
        },input('post.'));

        if(!$params){
            $this->error('非法参数');
        }

        PurchaseSub::create($params);

        $this->success('操作成功');
    }

    public function edit(){
        $id = input('get.id/d');

        if(!is_numeric($id)){
            $this->error('非法参数');
        }

        $res = PurchaseSub::where('id',$id)->find();
        

        return $this->fetch('',['vo'=>$res,'domain'=>request()->domain().'/upload/']);
    }

    public function editPost(){
        $params = array_map(function($v){
                    return is_array($v)?implode(',',$v):$v;
                    },input('post.'));

        if(!$params || empty($params['id'])){
            $this->error('非法参数');
        }

        PurchaseSub::update($params);

        $this->success('操作成功');
    }

    public function del(){
        $id = input('get.id/d');

        if(!(is_numeric($id) && $id>0)){
            $this->error('非法参数');
        }

        PurchaseSub::where('id',$id)->update(array('delete_time' => time(), 'admin_id' => cmf_get_current_admin_id()));

        $this->success('删除成功');
    }

    public function field(){
         $id = input('get.id/d');
         $field = input('get.field');

            if(!(is_numeric($id) && $id>0 && $field)){
            $this->error('非法参数');
        }

        $info = PurchaseSub::field($field)->where('id',$id)->find();

        $this->success('ok','',$info[$field]);
    }
    public function push(){
        $id = input('get.id/d');
        $res = PurchaseSub::where('id',$id)->find();
        $info=DB::name('coil')->where('coilno',$res['coil_num'])->count();
        if ($info>0){
            $this->error('下推失败,该条信息已钢卷库');
        }
//        DB::name('coil')->add()
        $this->success($info,'',$id);
    }
}